package com.algorithm.datastructure;

import java.util.Iterator;

/**
 * 双向环形链表(带哨兵)
 */
public class DoublyLinkedListSentinel2 implements Iterable<Integer> {

    private Node sentinel = new Node(null, -1, null);

    public DoublyLinkedListSentinel2() {
        sentinel.prev = sentinel;
        sentinel.next = sentinel;
    }

    private static class Node {
        Node prev;
        int value;
        Node next;

        public Node(Node prev, int value, Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p = sentinel.next;

            @Override
            public boolean hasNext() {
                return p != sentinel;
            }

            @Override
            public Integer next() {
                int value = p.value;
                p = p.next;
                return value;
            }
        };
    }

    public void addFirst(int value) {
        Node a = sentinel;
        Node b = sentinel.next;
        Node added = new Node(a, value, b);
        a.next = added;
        b.prev = added;
    }

    public void removeFirst() {
        Node removed = sentinel.next;
        if (removed == sentinel) {
            throw new IllegalArgumentException("非法");
        }
        Node b = removed.next;
        sentinel.next = b;
        b.prev = sentinel;
    }

    public void addLast(int value) {
        Node last = sentinel.prev;
        Node added = new Node(last, value, sentinel);
        last.next = added;
        sentinel.prev = added;
    }

    public void removeLast() {
        Node removed = sentinel.prev;
        if (removed == sentinel) {
            throw new IllegalArgumentException("非法");
        }
        Node a = removed.prev;
        a.next = sentinel;
        sentinel.prev = a;
    }

    public Boolean removeByValue(int value) {
        Node node = findByValue(value);
        if (node==null) {
            return false;
        }
        Node a = node.prev;
        Node b = node.next;
        a.next=b;
        b.prev=a;
        return true;
    }

    public Node findByValue(int value) {
        Node p =sentinel.next;
        while (p != sentinel) {
            if (p.value == value) {
                return p;
            }
            p = p.next;
        }
        return null;
    }

    public static void main(String[] args) {
        DoublyLinkedListSentinel2 doublyLinkedListSentinel2 = new DoublyLinkedListSentinel2();
        doublyLinkedListSentinel2.addLast(1);
        doublyLinkedListSentinel2.addFirst(2);
        doublyLinkedListSentinel2.addLast(3);
        doublyLinkedListSentinel2.removeFirst();
        doublyLinkedListSentinel2.removeLast();
        doublyLinkedListSentinel2.addLast(5);
        doublyLinkedListSentinel2.addLast(8);
        doublyLinkedListSentinel2.addLast(5);
        doublyLinkedListSentinel2.forEach(System.out::println);
        System.out.println("------------------");
        System.out.println("removed 5 :"+
                doublyLinkedListSentinel2.removeByValue(5));
        doublyLinkedListSentinel2.forEach(System.out::println);
    }
}
